﻿using HardcoreGame.Common.UI;
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using Terraria;
using Terraria.ModLoader;
using Terraria.UI;

namespace HardcoreGame.Common.Systems
{
    public class BookcaseUISystem : ModSystem
    {
        internal static UserInterface BookcaseInterface;
        internal static BookcaseUIState BookcaseUI;
        public override void Load()
        {
            if (!Main.dedServ)
            {
                BookcaseUI = new BookcaseUIState();
                BookcaseUI.Activate();
                BookcaseInterface = new UserInterface();
            }
        }
        public override void Unload()
        {
            BookcaseInterface = null;
            BookcaseUI = null;
        }
        public override void UpdateUI(GameTime gameTime)
        {
            UserInterface ui = BookcaseInterface;
            if ((ui?.CurrentState) == null)
            {
                return;
            }
            Main.LocalPlayer.mouseInterface = true;
            if (!Main.playerInventory)
            {
                ui.SetState(null);
                return;
            }
            ui.Update(gameTime);
        }
        public override void ModifyInterfaceLayers(List<GameInterfaceLayer> layers)
        {
            int index = layers.FindIndex((GameInterfaceLayer l) => l.Name == "Vanilla: Mouse Text");
            if (index < 0)
            {
                index = layers.Count;
            }
            layers.Insert(index, new LegacyGameInterfaceLayer("BookcaseInventory: UI", delegate
            {
                UserInterface ui = BookcaseInterface;
                if ((ui?.CurrentState) != null)
                {
                    Main.LocalPlayer.mouseInterface = true;
                    ui.Draw(Main.spriteBatch, new GameTime());
                }
                return true;
            }, (InterfaceScaleType)1));
        }
    }
}
